package com.eb.bi.common;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class PluginResult {
	private String m_resultFilePath;
	private String batchId;
	private String beginTime;
	private String endTime;
	private String exitCode;
	private String exitDesc;
	private String jobId;
	private String taskId;
	private String timeCosts;
	
	private HashMap<String,String> otherParam = new HashMap<String,String>();
	
	private static PluginResult PluginResult;
	private static PluginResult PluginResultInit(){
		if(PluginResult == null){
			PluginResult = new PluginResult();
		}
		return PluginResult;
	}
	public static PluginResult getInstance(){
		return PluginResultInit();
	}
	
	public boolean save(){
		try {
			FileWriter fw = new FileWriter(m_resultFilePath);
			BufferedWriter bw = new BufferedWriter(fw);
			bw.write("<?xml version=\"1.0\" encoding=\"GB2312\"?>\n");
			bw.write("<result>\n");
			bw.write("<param name=\"batchId\">"+batchId+"</param>\n");
			bw.write("<param name=\"beginTime\">"+beginTime+"</param>\n");
			bw.write("<param name=\"endTime\">"+endTime+"</param>\n");
			bw.write("<param name=\"exitCode\">"+exitCode+"</param>\n");
			bw.write("<param name=\"exitDesc\">"+exitDesc+"</param>\n");
			bw.write("<param name=\"jobId\">"+jobId+"</param>\n");
			bw.write("<param name=\"taskId\">"+taskId+"</param>\n");
			bw.write("<param name=\"timeCosts\">"+timeCosts+"</param>\n");
			if(otherParam.size() != 0){
				Iterator<Map.Entry<String, String>> iter = otherParam.entrySet().iterator();
				while(iter.hasNext()){
					Map.Entry<String, String> entry = iter.next();
					bw.write("<param name=\""+entry.getKey()+"\">"+entry.getValue()+"</param>\n");
				}
			}
			
			bw.write("</result>\n");
			bw.flush();
			fw.flush();
			bw.close();
			fw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return true;
	}
	
	
	public void addOtherParam(String key,String value){
		otherParam.put(key, value);
	}
	
	public String getM_resultFilePath() {
		return m_resultFilePath;
	}

	public void setM_resultFilePath(String filePath) {
		m_resultFilePath = filePath;
	}

	public String getBatchId() {
		return batchId;
	}

	public void setBatchId(String batchId) {
		this.batchId = batchId;
	}

	public String getBeginTime() {
		return beginTime;
	}

	public void setBeginTime(String beginTime) {
		this.beginTime = beginTime;
	}

	public String getEndTime() {
		return endTime;
	}

	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}

	public String getExitCode() {
		return exitCode;
	}

	public void setExitCode(String exitCode) {
		this.exitCode = exitCode;
	}

	public String getExitDesc() {
		return exitDesc;
	}

	public void setExitDesc(String exitDesc) {
		this.exitDesc = exitDesc;
	}

	public String getJobId() {
		return jobId;
	}

	public void setJobId(String jobId) {
		this.jobId = jobId;
	}

	public String getTaskId() {
		return taskId;
	}

	public void setTaskId(String taskId) {
		this.taskId = taskId;
	}

	public String getTimeCosts() {
		return timeCosts;
	}

	public void setTimeCosts(String timeCosts) {
		this.timeCosts = timeCosts;
	}

	public void setFilePath(String filePath) {
		m_resultFilePath = filePath;
	}
	
}
